package com.example.demo.controller;

import com.example.demo.model.Userinfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/add")
    @Transactional
    public int add() {
        // 1.构造对象
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername("李四");
        userinfo.setPassword("123");
        // 2.调用 service 执行添加
        int result = userService.add(userinfo);
        System.out.println("result：" + result);
        try{
            int i = 10 / 0;
        } catch (Exception e) {
            e.getMessage();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }


        // 3.将结果给前端
        return result;
    }

}
